{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "02155.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yananma/5_programs_per_day/blob/master/02155.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bndABGtjZbo1",
        "colab_type": "text"
      },
      "source": [
        "## 6.2 循环神经网络"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zNV1IzVaalBE",
        "colab_type": "text"
      },
      "source": [
        "### 6.2.2 含隐含状态的循环神经网络"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "b4lUCKrLatGz",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "6dab5b74-670e-4c01-d1db-c61dacb76bad"
      },
      "source": [
        "import torch \n",
        "\n",
        "X, W_xh = torch.randn(3, 1), torch.randn(1, 4)\n",
        "H, W_hh = torch.randn(3, 4), torch.randn(4, 4)\n",
        "torch.matmul(X, W_xh) + torch.matmul(H, W_hh)"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.2489, -1.0347, -1.8338,  2.5576],\n",
              "        [ 0.2560,  0.6495, -0.3181,  2.0889],\n",
              "        [ 2.3568, -0.3214,  4.1006, -4.7678]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 1
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-xHGihr6uy1O",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "0df7f12f-1506-4ce1-8268-2268deabc3b5"
      },
      "source": [
        "torch.matmul(torch.cat((X, H), dim=1), torch.cat((W_xh, W_hh), dim=0))"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.2489, -1.0347, -1.8338,  2.5576],\n",
              "        [ 0.2560,  0.6495, -0.3181,  2.0889],\n",
              "        [ 2.3568, -0.3214,  4.1006, -4.7678]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 3
        }
      ]
    }
  ]
}